package com.shizhanzhe.zxbapp.team;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.TeamMoney;
import com.shizhanzhe.zxbapp.bean.TeamVIP;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.bean.Will;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zz9527 on 2018/11/28.
 */
public class TeamDeatilActivity extends Activity {
    private int type = 0;
    private View ts;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teamdetail);
        ImmersionBar.with(this).statusBarColor(R.color.them)
                .fitsSystemWindows(true).init();
        TextView title = findViewById(R.id.team_title);
        final GridView lv = findViewById(R.id.team_lv);
        type = getIntent().getIntExtra("type", 0);
        findViewById(R.id.backarea).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        ts = findViewById(R.id.ts);
        switch (type) {
            case 1:
                title.setText("今日团队收入");
                break;
            case 2:
                title.setText("本月团队收入");
                break;
            case 3:
                title.setText("累积团队收入");
                break;
            case 4:
                title.setText("今日新增VIP");
                break;
            case 5:
                title.setText("本月新增VIP");
                break;
            case 6:
                title.setText("团队VIP");
                break;
            case 7:
                title.setText("团队成员");
                break;
            case 8:
                title.setText("预计收入");
                break;
            default:
                break;
        }
        if (type < 4 || type == 7) {
            OkHttpDownloadJsonUtil.downloadJson(this, Path.TeamDetail(UserBean.uid, 1), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

                @Override
                public void onsendJson(String json) {
                    try {
                        Gson gson = new Gson();
                        TeamMoney.DataBean data = gson.fromJson(json, TeamMoney.class).getData();
                        TeamMoney.DataBean.OneselfBean oneself = data.getOneself();
                        TeamMoney.DataBean.TeamsBean b = new TeamMoney.DataBean.TeamsBean(oneself.getNickname(), oneself.getAvatar(), oneself.getDaybalance(), oneself.getMonthbalance(), oneself.getTotalbalance(), oneself.getViptime());
                        List<TeamMoney.DataBean.TeamsBean> list = new ArrayList<>();
                        if (Double.parseDouble(b.getDaybalance()) != 0 && type == 1) {
                            list.add(b);
                        } else if (Double.parseDouble(b.getMonthbalance()) != 0 && type == 2) {
                            list.add(b);
                        } else if (Double.parseDouble(b.getTotalbalance()) != 0 && type == 3) {
                            list.add(b);
                        } else if (type == 7) {
                            list.add(b);
                            lv.setNumColumns(4);
                        }
                        List<TeamMoney.DataBean.TeamsBean> teams = data.getTeams();
                        for (TeamMoney.DataBean.TeamsBean t : teams) {
                            if (Double.parseDouble(t.getDaybalance()) != 0 && type == 1) {
                                list.add(t);
                            } else if (Double.parseDouble(t.getMonthbalance()) != 0 && type == 2) {
                                list.add(t);
                            } else if (Double.parseDouble(t.getTotalbalance()) != 0 && type == 3) {
                                list.add(t);
                            } else if (type == 7) {
                                list.add(t);
                            }
                        }
                        if (list.size() == 0) {
                            Toast.makeText(TeamDeatilActivity.this, "暂无数据", Toast.LENGTH_SHORT).show();
                        }
                        lv.setAdapter(new TeamAdapter(TeamDeatilActivity.this, list, type));
                    } catch (Exception e) {
                    }
                }
            });
        } else if (type == 8) {
            ts.setVisibility(View.VISIBLE);
            OkHttpDownloadJsonUtil.downloadJson(this, Path.WILL(UserBean.uid), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {


                @Override
                public void onsendJson(String json) {
                    try {
                        List<TeamMoney.DataBean.TeamsBean> list = new ArrayList<>();
                        Gson gson = new Gson();
                        Will will = gson.fromJson(json, Will.class);
                        Will.ZhuanBean zhuan = will.getZhuan();
                        Will.Daxia daxia = will.getDaxia();
                        if (daxia!=null){
                            list.add(new TeamMoney.DataBean.TeamsBean("VIP间接推广收益", "", "", "", daxia.getDan() + "", 0));
                        }
                        if (zhuan != null) {
                            list.add(new TeamMoney.DataBean.TeamsBean(zhuan.getNickename(), zhuan.getAvatar(), "", "", zhuan.getDan() + "", 0));
                        }
                        List<Will.DataBean> data = will.getData();
                        for (Will.DataBean d : data) {
                            list.add(new TeamMoney.DataBean.TeamsBean(d.getNickename(), d.getAvatar(), "", "", d.getDan(), 0));
                        }
                        if (list.size() == 0) {
                            Toast.makeText(TeamDeatilActivity.this, "暂无数据", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        lv.setAdapter(new TeamAdapter(TeamDeatilActivity.this, list, type));
                    } catch (Exception e) {
                    }
                }
            });
        } else {
            OkHttpDownloadJsonUtil.downloadJson(this, Path.TeamDetail(UserBean.uid, type - 2), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {


                @Override
                public void onsendJson(String json) {
                    List<TeamMoney.DataBean.TeamsBean> list = new ArrayList<>();
                    Gson gson = new Gson();
                    TeamVIP teamVIP = gson.fromJson(json, TeamVIP.class);
                    try {
                        TeamVIP.DataBean.OwnBean own = teamVIP.getData().getOwn();
                        if (!TextUtils.isEmpty(own.getNickname())) {
                            TeamMoney.DataBean.TeamsBean b = new TeamMoney.DataBean.TeamsBean(own.getNickname(), own.getAvatar(), "", "", "", own.getViptime());
                            list.add(b);
                        }
                        List<TeamVIP.DataBean.TeamsBean> teams = teamVIP.getData().getTeams();
                        for (TeamVIP.DataBean.TeamsBean t : teams) {
                            list.add(new TeamMoney.DataBean.TeamsBean(t.getNickname(), t.getAvatar(), "", "", "", t.getViptime()));
                        }
                        if (list.size() == 0) {
                            Toast.makeText(TeamDeatilActivity.this, "暂无数据", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        lv.setAdapter(new TeamAdapter(TeamDeatilActivity.this, list, type));
                    } catch (Exception e) {
                        List<TeamVIP.DataBean.TeamsBean> teams = teamVIP.getData().getTeams();
                        for (TeamVIP.DataBean.TeamsBean t : teams) {
                            list.add(new TeamMoney.DataBean.TeamsBean(t.getNickname(), t.getAvatar(), "", "", "", t.getViptime()));
                        }
                        if (list.size() == 0) {
                            Toast.makeText(TeamDeatilActivity.this, "暂无数据", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        lv.setAdapter(new TeamAdapter(TeamDeatilActivity.this, list, type));
                    }
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
